package payment.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import payment.QRCodeUtils;

public class QRCode implements HttpHandler {
    @Override
    public void handle(HttpExchange exchange) throws IOException {
        String content = exchange.getRequestURI().toString().replace("/qrcode?content=", "");
        exchange.sendResponseHeaders(200, 0);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        if (QRCodeUtils.createCodeToOutputStream(content, baos)) {
            exchange.getResponseHeaders().add("Content-Type", "image/png");
            exchange.getResponseBody().write(baos.toByteArray());
        }
        exchange.close();
    }
}
